cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(omnibus)

# Version  ## TODO Versionsnummer ändern
set(omnibus_VERSION_MAJOR 0)
set(omnibus_VERSION_MINOR 2)
set(omnibus_VERSION_PATCH 5)
set(omnibus_VERSION ${omnibus_VERSION_MAJOR}.${omnibus_VERSION_MINOR}.${omnibus_VERSION_PATCH})

message(STATUS "Configuring omnibus version ${omnibus_VERSION}")

list(APPEND CMAKE_MODULE_PATH "${omnibus_SOURCE_DIR}/cmake_modules")

# Provide some choices for CMAKE_BUILD_TYPE
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
    "" Release Debug RelWithDebInfo MinSizeRel)


# Option to produce statically linked binaries
option(BUILD_STATIC_BINS
    "Whether to link executables statically.
If selected overrrides BUILD_SHARED_LIBS.
This option has to be set BEFORE the initial creation of the CMake cache
(this is only possible by using the non-interactive command line interface
`cmake ../ -DBUILD_STATIC_BINS=ON`)
Otherwise the library paths will not be rediscovered."
    OFF
    )

if(DEFINED BUILD_STATIC_BINS_PREVIOUS_VALUE AND
        ((BUILD_STATIC_BINS_PREVIOUS_VALUE AND NOT BUILD_STATIC_BINS) OR
        (NOT BUILD_STATIC_BINS_PREVIOUS_VALUE AND BUILD_STATIC_BINS)))
    message(FATAL_ERROR
        "You can't set BUILD_STATIC_BINS to a different value after the "
        "CMake cache has been built because then the library paths will not "
        "be rediscovered. "
        "Setting this before cache creation is only possible by using the "
        "non-interactive command line interface: "
        "`cmake ../ -DBUILD_STATIC_BINS=ON`"
        )
endif()
set(BUILD_STATIC_BINS_PREVIOUS_VALUE "${BUILD_STATIC_BINS}" CACHE INTERNAL
    "Internal value to detect whether the BUILD_STATIC_BINS has been previously
    set to a different value"
    )

if(BUILD_STATIC_BINS)
    set(BUILD_SHARED_LIBS OFF)
    set(Boost_USE_STATIC_LIBS ON)
    set(Boost_USE_STATIC_RUNTIME ON)
    # Give priority to .a files (or equivalent)
    set(CMAKE_FIND_LIBRARY_SUFFIXES
            ${CMAKE_STATIC_LIBRARY_SUFFIX} ${CMAKE_FIND_LIBRARY_SUFFIXES})
endif(BUILD_STATIC_BINS)
# End of option to produce statically linked binaries


### LIBS

# Find Boost
find_package(BoostAdditionalVersions QUIET)
if(NOT BoostAdditionalVersions_FOUND)
    message(WARNING "Could not find FindBoostAdditionalVersions.cmake. "
        "This might cause the Boost detection to fail")
endif()
find_package(Boost REQUIRED COMPONENTS system filesystem date_time)
if(NOT Boost_FOUND)
    message(FATAL_ERROR "Could not find Boost. "
            "Please have a look at the Boost_* cache variables.")
endif(NOT Boost_FOUND)

mark_as_advanced(Boost_DIR)

include_directories(${Boost_INCLUDE_DIRS})
list(APPEND omnibus_LINK_LIBRARIES ${Boost_LIBRARIES})


# Find Ariba
find_package(ariba 0.8.1 REQUIRED)
if(NOT ariba_FOUND)
    message(FATAL_ERROR "Could not find ariba. "
            "Please have a look at the ariba_* cache variables.")
endif(NOT ariba_FOUND)

include_directories(${ariba_INCLUDE_DIRS})
list(APPEND omnibus_LINK_LIBRARIES ariba ariba_dht)


### BIN
add_executable(omnibus-bin
 ## TOP LEVEL
 Access_attributes.cpp Access_attributes.h Chat.h Chat.cpp FileInfo.cpp FileInfo.h Filetransfer.cpp Filetransfer.h Flow_control.cpp Flow_control.h main.cpp Network.cpp Network.h Phrase_connector.cpp Phrase_connector.h Transport.cpp Transport.h UI_multiplexer.cpp UI_multiplexer.h User.cpp Userdatabase.cpp Userdatabase.h User.h Config.h Config.cpp ARQQueue.h ARQQueue.cpp Readahead_manager.h Readahead_manager.cpp

 ## EXCEPTIONS
 exceptions/not_found_exception.cpp exceptions/not_found_exception.h

 ## INTERFACES
 interfaces/DhtInterface.h interfaces/ConnStatInterface.h interfaces/LinkEstablishedInterface.h interfaces/MessageHandlerInterface.h interfaces/Userinputinterface.h interfaces/Useroutputinterface.h
 
 ## LIBS
 libs/reboost_buffers/buffer.hpp libs/reboost_buffers/buffer.cpp libs/reboost_buffers/buffers.hpp libs/reboost_buffers/message.hpp libs/reboost_buffers/message.cpp libs/reboost_buffers/shared_buffer.hpp libs/reboost_buffers/shared_buffer.cpp libs/substreambuf/prefix_streambuf.hpp
 
 ## MESSAGES
 messages/ChatMessage.cpp messages/ChatMessage.h messages/ExtendedChatMessage.cpp messages/ExtendedChatMessage.h messages/ChatAck.cpp messages/ChatAck.h messages/Filetransfer messages/MeetMessage.cpp messages/MeetMessage.h messages/TransportMessage.cpp messages/TransportMessage.h messages/ConnectMessage.h messages/ConnectMessage.cpp messages/ConnectConfirmMessage.h messages/ConnectConfirmMessage.cpp

 
 ## MESSAGES (FILETRANSFER)
 messages/Filetransfer/PauseMessage.h messages/Filetransfer/PauseMessage.cpp messages/Filetransfer/AcceptMessage.cpp messages/Filetransfer/AcceptMessage.h messages/Filetransfer/CustodyRequestMessage.cpp messages/Filetransfer/CustodyRequestMessage.h messages/Filetransfer/DeclineMessage.cpp messages/Filetransfer/DeclineMessage.h messages/Filetransfer/PayloadMessage.cpp messages/Filetransfer/PayloadMessage.h messages/Filetransfer/PullAcceptMessage.cpp messages/Filetransfer/PullAcceptMessage.h messages/Filetransfer/PullRequestMessage.cpp messages/Filetransfer/PullRequestMessage.h messages/Filetransfer/PushRequestMessage.cpp messages/Filetransfer/PushRequestMessage.h messages/Filetransfer/BundleInfoMessage.cpp messages/Filetransfer/BundleInfoMessage.h
 
 ## SYSTEM EVENTS (kommen wohl wieder weg..)
 SystemEvents/Filetransfer/ResumeTransferEvent.h SystemEvents/Filetransfer/ResumeTransferEvent.cpp
 
 ## SYNC
 sync/Dir_syncer.h sync/Dir_syncer.cpp sync/Sync.h sync/Sync.cpp sync/Sync_File.h sync/Sync_File.cpp sync/Sync_Directory.h sync/Sync_Directory.cpp sync/Sync_Regular_File.h sync/Sync_Regular_File.cpp

 sync/messages/SyncInitMessage.h sync/messages/SyncInitMessage.cpp sync/messages/OfferMessage.h sync/messages/OfferMessage.cpp sync/messages/SyncControlMessage.cpp sync/messages/SyncControlMessage.h
 
 sync/interfaces/ScanCompletedInterface.h sync/interfaces/SyncUpdatedInterface.h
)



if(${BUILD_STATIC_BINS})
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc")
    string(REPLACE "-rdynamic" "" 
        CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS
        "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS}"
        )
    # Compile system libraries statically
    set_target_properties(omnibus-bin PROPERTIES
            LINK_SEARCH_END_STATIC ON)
endif(${BUILD_STATIC_BINS})


### LINKER
target_link_libraries(omnibus-bin ${omnibus_LINK_LIBRARIES})


### INSTALL
INSTALL(TARGETS omnibus-bin
	RUNTIME DESTINATION bin
)



## Add build type: Profiling ##
SET( CMAKE_CXX_FLAGS_PROFILING "-O2 -g -pg" CACHE STRING
    "Flags used by the C++ compiler during profiling builds."
    FORCE )
SET( CMAKE_C_FLAGS_PROFILING "-O2 -g -pg" CACHE STRING
    "Flags used by the C compiler during profiling builds."
    FORCE )
SET( CMAKE_EXE_LINKER_FLAGS_PROFILING
    "" CACHE STRING
    "Flags used for linking binaries during profiling builds."
    FORCE )
SET( CMAKE_SHARED_LINKER_FLAGS_PROFILING
    "" CACHE STRING
    "Flags used by the shared libraries linker during profiling builds."
    FORCE )
MARK_AS_ADVANCED(
    CMAKE_CXX_FLAGS_PROFILING
    CMAKE_C_FLAGS_PROFILING
    CMAKE_EXE_LINKER_FLAGS_PROFILING
    CMAKE_SHARED_LINKER_FLAGS_PROFILING )
# Update the documentation string of CMAKE_BUILD_TYPE for GUIs
#SET( CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
#    "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Profiling."
#    FORCE )
## [Add build type: Profiling] ##

## set default build type
IF(NOT CMAKE_BUILD_TYPE)
   SET(CMAKE_BUILD_TYPE Release
       CACHE STRING "Choose the type of build : None Debug Release RelWithDebInfo MinSizeRel Profiling."
       FORCE)
ENDIF(NOT CMAKE_BUILD_TYPE)
message("---> Current build type is: ${CMAKE_BUILD_TYPE}")
